<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    /*重新设置样式*/
    .form-control {
        width: 196px !important;
        height: 34px !important;
        padding: 10px !important;
    }
</style>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>厂级计划编辑与反馈</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="inBillForm" class="form-horizontal" role="form"
                          enctype="multipart/form-data">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group"
                                     style="line-height: 40px;margin: 0px;padding: 0px">
                                    <label>生产令编号：</label>
                                    <label th:text="${prodOrd.prodNo}"></label>
                                    <input type="hidden" id="profProdNo" name="profProdNo"
                                           th:value="${prodOrd.prodNo}" style="display: none" class="form-control"
                                           readonly="readonly"/>
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>项目名称：</label>
                                    <label th:text="${project.proName}"></label>
                                    <!--                                    <input type="text" th:value="${project.proName}" class="form-control" readonly="readonly"/>-->
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>数量：</label>
                                    <label th:text="${prodOrd.prodNum}"></label>
                                    <!--                                    <input type="text" th:value="${prodOrd.prodNum}" class="form-control" readonly="readonly"/>-->
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>编辑人所在部门：</label>
                                    <label th:if="${dept}!= null" th:text="${dept.name}"></label>
                                    <label th:if="${dept}== null">无</label>
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>编辑人：</label><label th:text="${user.name}"></label>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <div id="hot"/>
                </div>
                <!--                <div class="box-footer">-->
                <!--                    <div class="row" style="line-height: 34px;">-->
                <!--                        <div class="col-md-2">-->
                <!--                            <label>编辑人：</label><label th:text="${user.name}"></label>-->
                <!--                        </div>-->
                <!--                        <div class="col-md-6">-->

                <!--                        </div>-->
                <!--                        <div class="col-md-4">-->
                <!--                            <button type="button" id="btn-save" style="width: 82px;height: 34px"-->
                <!--                                    shiro:hasAnyPermissions="pro:productPlan:edit"-->
                <!--                                    class="btn btn-success file-contract btn-file pull-right">保存-->
                <!--                            </button>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                </div>-->
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    // /**
    //  *@FunctionName: getNewOrderCode
    //  *@Description: 定时获取新订单编码
    //  *@Author: 徐一贺
    //  *@CreateDate: 2020/3/23 9:34
    //  *@Phone: 18210416791
    //  *@Version: 1.0.0
    //  */
    // function getNewOrderCode() {
    //     setTimeout(getNewOrderCode, 5 * 1000);
    //     js.post({
    //         url: ctx + "purWarInManage/getWarInOrderCode",
    //         data: {odrCode: $("#odrCode").val()},
    //         contentType: "application/json",
    //         success: function (result) {
    //             if (result.msg == 'SUCCESS') {
    //                 $("#odrCode").val(result.data);
    //             }
    //         }
    //     });
    // }
    /**
     * 验证日期格式
     * @param date
     * @return {boolean}
     */
    function checkDate(date) {
        var res = false;
        if (date != undefined && date != null && date != '') {
            var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
            if (result == null)
                return false;
            var d = new Date(result[1], result[3] - 1, result[4]);
            res = (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);
        } else {
            res = true;
        }
        if (res == false) {
            js.modal.warning("红色背景色的记账日期格式错误，请修改。正式的格式是：“年\\月\\日”。例如：“2020\\06\\01”。还有请按照正常日历的年份、月份和日期填写。");
        }
        return res;
    }

    function reLoad() {
        $.post(
            ctx + 'task/getFactoryPlanEditAndReturnListByProfProdNo',
            {
                profProdNo: $("#profProdNo").val(),
            },
            function (result) {
                let hotData = result.data.insertData;
                js.hot.loadData(hotData);
            }
        );
    }

    $(function () {
        $.post(
            ctx + 'task/getFactoryPlanEditAndReturnListByProfProdNo',
            {
                profProdNo: $("#profProdNo").val(),
            },
            function (result) {
                let hotData = result.data.insertData;
                var process = function (data) {
                    if ($.isEmptyObject(data.insertData)) {
                        js.modal.warning("厂级计划编辑与反馈不允许为空！");
                        return;
                    }
                    var checkData = data.insertData;
                    var errorInfoArray = new Array();
                    var errorCount = 0;
                    for (var i = 0; i < checkData.length; i++) {
                        if (checkData[i].profFac == undefined || checkData[i].profFac == "") {
                            errorInfoArray.push("第" + (i + 1) + "行 分配车间/组/人员不允许为空！。<br>");
                            errorCount++;
                        }
                        if (checkData[i].profName == undefined || checkData[i].profName == "") {
                            errorInfoArray.push("第" + (i + 1) + "行 任务名称不允许为空！<br>");
                            errorCount++;
                        }
                        if (checkData[i].profStaDat == undefined || checkData[i].profStaDat == "") {
                            errorInfoArray.push("第" + (i + 1) + "行 开始时间不允许为空！<br>");
                            errorCount++;
                        }
                        if (checkData[i].profEndDat == undefined || checkData[i].profEndDat == "") {
                            errorInfoArray.push("第" + (i + 1) + "行 结束时间不允许为空！<br>");
                            errorCount++;
                        }
                    }
                    if (errorCount > 0) {
                        errorInfoArray.push("计划保存失败，请先维护以上计划信息！");
                        js.modal.warning(errorInfoArray.join(''));
                        return;
                    }
                    js.post({
                        url: ctx + "task/saveFactoryPlanEditAndReturnList",
                        data: JSON.stringify(data),
                        contentType: "application/json",
                        success: function (result) {
                            if (result.type == "SUCCESS") {
                                reLoad();
                                js.modal.success(result.data);
                            } else {
                                js.modal.warning(result.data);
                            }
                        }
                    });
                }
                js.hot.init({
                    element: document.getElementById('hot'),
                    data: {},
                    formData: ["profProdNo"],
                    colHeaders: ['profId', '分配车间/组/人员', '任务名称', '开始时间', '结束时间', '历时（天）', '备注','标识','最后编辑时间'],
                    fieldName: ['profId', 'profFac', 'profName', 'profStaDat', 'profEndDat', 'duration', 'profRem','flag', 'profEdiDat'],
                    height: 640,
                    minRows: 100,
                    hiddenColumns: {
                        columns: [0, 8],
                        indicators: true
                    },
                    callBack: process,
                    afterChange: function (changes, source) {           // 修改后执行
                        if (changes != null) {
                            let index = changes[0][0];//行号码
                            let rowData = js.hot._hot.getDataAtRow(index);
                            let data = {};
                            let start;
                            let end;
                            let duration;
                            $.each(js.hot._option.fieldName, function (index, name) {
                                if (name === 'profId') {
                                    start = null;
                                    end = null;
                                    duration = null;
                                    data[name] = rowData[index];
                                    data['profProdNo'] = $("#profProdNo").val();
                                }
                                if (name === 'profFac') {
                                    if (rowData[index] != '') {
                                        data[name] = rowData[index];
                                    } else {
                                        data[name] = null;
                                    }
                                }
                                if (name === 'profName') {
                                    if (rowData[index] != '') {
                                        data[name] = rowData[index];
                                    } else {
                                        data[name] = null;
                                    }
                                }
                                if (name === 'profRem') {
                                    if (rowData[index] != '') {
                                        data[name] = rowData[index];
                                    } else {
                                        data[name] = null;
                                    }
                                }
                                if (name === 'profStaDat') {
                                    if (rowData[index] != '') {
                                        data[name] = rowData[index];
                                    } else {
                                        data[name] = null;
                                    }

                                }
                                if (name === 'profStaDat' && rowData[index] != undefined && rowData[index].indexOf('/') > -1) {
                                    start = new Date(rowData[index].replace('/', '-'));
                                }
                                if (name === 'profEndDat') {
                                    if (rowData[index] != '') {
                                        data[name] = rowData[index];
                                    } else {
                                        data[name] = null;
                                    }
                                }
                                if (name === 'profEndDat' && rowData[index] != undefined && rowData[index].indexOf('/') > -1) {
                                    data[name] = rowData[index];
                                    end = new Date(rowData[index].replace('/', '-'));
                                }
                                if (name === 'profEndDat' && start != undefined && start != null && start != '' && end != undefined && end != null && end != '') {
                                    duration = Math.floor((end - start) / 1000 / 60 / 60 / 24);
                                }
                                if (name === 'duration') {
                                    data[name] = duration;
                                }
                                if (name === 'flag') {
                                    if (rowData[index] != '') {
                                        data[name] = '有数据';
                                    } else {
                                        data[name] = null;
                                    }
                                }
                                if (name === 'profEdiDat') {
                                    let month = eval(new Date().getMonth() + 1);
                                    let monthStr = '';
                                    if (month < 10) {
                                        monthStr = '0' + month;
                                    } else {
                                        monthStr = '' + month;
                                    }
                                    let day = new Date().getDate();
                                    let dayStr = '';
                                    if (day < 10) {
                                        dayStr = '0' + day;
                                    } else {
                                        dayStr = '' + day;
                                    }
                                    let hours = new Date().getHours();
                                    let hoursStr = '';
                                    if (hours < 10) {
                                        hoursStr = '0' + hours;
                                    } else {
                                        hoursStr = '' + hours;
                                    }
                                    let minutes = new Date().getMinutes();
                                    let minutesStr = '';
                                    if (minutes < 10) {
                                        minutesStr = '0' + minutes;
                                    } else {
                                        minutesStr = '' + minutes;
                                    }
                                    let seconds = new Date().getSeconds();
                                    let secondsStr = '';
                                    if (seconds < 10) {
                                        secondsStr = '0' + seconds;
                                    } else {
                                        secondsStr = '' + seconds;
                                    }
                                    data[name] = new Date().getFullYear() + '-' + monthStr + '-' + dayStr + ' ' + hoursStr + ':' + minutesStr + ':' + secondsStr;
                                }
                            });

                            $.ajax({
                                url: ctx + "task/saveFactoryPlanEditAndReturnList",
                                type: 'post',
                                traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                                dataType: 'json',
                                contentType: "application/x-www-form-urlencoded",
                                data: {data: JSON.stringify(data)},
                                async: true,                // 异步执行
                                success: function (result) {
                                    if (result.type === web_status.SUCCESS) {
                                        reLoad();
                                    } else {
                                        js.modal.warning(result.msg);
                                    }
                                },
                                error: function () {
                                    js.modal.fail();
                                }
                            });
                        }
                    },
                    beforeRemoveRow: function (index, amount) {         // 删除行时执行， 参数 index 第一行索引， amount 行数
                        if (js.hot._option.removeCallBack) {
                            js.hot._option.removeCallBack(index, amount);
                        }
                        // 封装id成array传入后台
                        if (amount !== 0) {
                            for (var i = index; i < amount + index; i++) {
                                var rowdata = js.hot._hot.getDataAtRow(i);
                                if (rowdata[0] != null) {
                                    $.ajax({
                                        url: ctx + "task/removeFactoryPlanEditAndReturnListByProfId",
                                        type: 'post',
                                        traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                                        dataType: 'json',
                                        contentType: "application/x-www-form-urlencoded",
                                        data: {profId: rowdata[0]},
                                        async: true,                // 异步执行
                                        success: function (result) {
                                            if (result.type === web_status.SUCCESS) {
                                                reLoad();
                                            } else {
                                                js.modal.warning(result.msg);
                                            }
                                        },
                                        error: function () {
                                            js.modal.fail();
                                        }
                                    });
                                }
                            }
                        }
                    }
                    ,
                    columns: [
                        {
                            data: 'profId',
                            type: 'numeric',
                            width: 40
                        },
                        {
                            data: 'profFac',//分配车间/组/人员
                            type: 'text'
                        },
                        {
                            data: 'profName',//任务名称
                            type: 'text'
                        },
                        {
                            data: 'profStaDat',//开始时间
                            type: 'date',
                            dateFormat: 'YYYY-MM-DD',
                            correctFormat: true,
                            validator(val, callback) {
                                let pattern = /^.{0,20}$/
                                if (checkDate(val)) return callback(true)
                                return callback(false)
                            }
                        },
                        {
                            data: 'profEndDat',//结束时间
                            type: 'date',
                            dateFormat: 'YYYY-MM-DD',
                            correctFormat: true,
                            validator(val, callback) {
                                let pattern = /^.{0,20}$/
                                if (checkDate(val)) return callback(true)
                                return callback(false)
                            }
                        },
                        {
                            data: 'duration',//历时（天）
                            type: 'numeric'
                        },
                        {
                            data: 'profRem',//备注
                            type: 'text'
                        },
                        {
                            data: 'flag',//标识
                            type: 'text',
                            readOnly: true
                        },
                        {
                            data: 'profEdiDat',//最后编辑时间
                            type: 'text'
                        }
                    ]
                });

                js.hot.loadData(hotData);
            }
        )
    });
</script>
</body>
</html>
